﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ConfigFile.cs" company="Data Ductus Malmö">
//   (c) 2012 Data Ductus Malmö AB
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace DataDuctus.Snmp.EmbeddableNetSnmp
{
  using System;
  using System.Collections.Generic;
  using System.Globalization;
  using System.IO;
  using System.Text;

  /// <summary>
  /// Represents a snmpd.conf file.
  /// </summary>
  public sealed class ConfigFile
  {
    /// <summary>
    /// Initializes a new instance of the <see cref="ConfigFile"/> class.
    /// </summary>
    public ConfigFile()
    {
      this.RoCommunities = new List<string>();
      this.RwCommunities = new List<string>();
      this.SysLocation = "Unknown";
      this.SysContact = "Unknown <root@unkown.net>";
      this.SysServices = 76;
    }

    /// <summary>
    /// Gets the rw communities.
    /// </summary>
    public List<string> RwCommunities { get; private set; }

    /// <summary>
    /// Gets the ro communities.
    /// </summary>
    public List<string> RoCommunities { get; private set; }

    /// <summary>
    /// Gets or sets the persistence base directory.
    /// </summary>
    public string PersistenceBaseDirectory { get; set; }

    /// <summary>
    /// Gets or sets the sys location.
    /// </summary>
    public string SysLocation { get; set; }

    /// <summary>
    /// Gets or sets the sys contact.
    /// </summary>
    public string SysContact { get; set; }

    /// <summary>
    /// Gets or sets the sys services.
    /// </summary>
    public int SysServices { get; set; }

    /// <summary>
    /// Renders the properties into a snmpd.conf file format.
    /// </summary>
    /// <returns>
    /// A string representation of a snmp.conf file.
    /// </returns>
    public string RenderConfig()
    {
      var sb = new StringBuilder();

      sb.Append("#AutoGenerated ").Append(DateTime.UtcNow.ToString(CultureInfo.InvariantCulture)).AppendLine(" (utc)");

      if (!string.IsNullOrEmpty(this.PersistenceBaseDirectory))
      {
        sb.Append("[snmp] persistentDir ").AppendLine(this.PersistenceBaseDirectory.Replace(Path.DirectorySeparatorChar, '/'));
      }

      foreach (var community in this.RoCommunities)
      {
        sb.Append("rocommunity ").AppendLine(community);
      }

      foreach (var community in this.RwCommunities)
      {
        sb.Append("rwcommunity ").AppendLine(community);
      }

      sb.AppendLine("proc 0 0");

      sb.Append("syslocation ").AppendLine(this.SysLocation);
      sb.Append("syscontact ").AppendLine(this.SysContact);
      sb.Append("sysservices ").AppendLine(this.SysServices.ToString(CultureInfo.InvariantCulture));

      return sb.ToString();
    }

    /// <summary>
    /// Writes the snmpd.conf file onto disc.
    /// </summary>
    /// <param name="fullyQualifiedFilePath">
    /// The fully qualified filepath to the actual file to be written.
    /// </param>
    /// <remarks>
    /// If any file exist it will be truncated and overwritten.
    /// </remarks>
    public void WriteConfig(string fullyQualifiedFilePath)
    {
      var config = Encoding.ASCII.GetBytes(this.RenderConfig());

      using (var strm = new FileStream(fullyQualifiedFilePath,FileMode.Create,FileAccess.Write,FileShare.Read))
      {
        strm.Write(config, 0, config.Length);
      }
    }
  }
}
